package ua.khpi.si.kit49.diploma.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import ua.khpi.si.kit49.diploma.db.dto.City;

/**
 * Class for store all cities with weather and last update date for weather. The
 * goal of this class is avoid repetitions of sending requests to the weather
 * service. Request makes occurs only once per day for every city.
 * 
 * @author netstalk3r
 * 
 */

public class CityHolder {

	private List<City> listOfCities;
	private Date lastUpdateDate;

	private static final Logger log = Logger.getLogger(CityHolder.class);

	private static final CityHolder instance = new CityHolder();

	private CityHolder() {
		super();
	}

	public static CityHolder getInstance() {
		return instance;
	}

	public List<City> getListOfCities() {
		return listOfCities;
	}

	public synchronized void setListOfCities(List<City> listOfCities) {
		this.listOfCities = listOfCities;
	}

	public Date getLastUpdateDate() {
		return lastUpdateDate;
	}

	public synchronized void setLastUpdateDate(Date lastUpdateDate) {
		this.lastUpdateDate = lastUpdateDate;
	}

	public boolean isListEmpty() {
		if ((listOfCities == null) || (listOfCities.size() == 0)) {
			return true;
		} else {
			return false;
		}
	}

	public Integer getAmountOfCity() {
		return listOfCities.size();
	}

	public City getCityById(Integer id) {
		log.debug("Searching for city with id = " + id);
		City city = null;
		for (int i = 0; i < listOfCities.size(); i++) {
			Integer currentId = listOfCities.get(i).getId();
			if (id.equals(currentId)) {
				city = listOfCities.get(i);
				break;
			}
		}
		log.debug("city for return: " + city);
		return city;
	}
}
